import * as io from '../services/socketio'

export default {

  namespace: 'global',

  state: {
    name: 'ssssssssssssss',
    user: {
      isLogin: true,
      name: 'admin100',
    }
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

  effects: {
    *fetch({ payload }, { put }) {
      yield put({ type: 'save' });
    },
  },

  subscriptions: {
    // 订阅SOCKET消息事件
    socketio({ dispatch }) {
      io.listen(action => {
        switch (action.type) {
          // 监听链接成功事件
          case 'connect':
            msg(action.state, action.msg)
            dispatch({ type: 'sendInitMsg' })
            break;

          // 处理初始化数据
          case 'init':
            dispatch({ type: 'save', payload: action.data })
            break;


          // 处理消息提示事件
          default:
            msg(action.state, action.msg)
            break;
        }
      })
    }
  },

};